import type { Area, CaseSubCategory, HomeBannerItem, LawyerComment, LawyerDetail, LawyerDetailV2, LawyerSearchHit, LegalCategory, LegalFirstClass, LegalSecondClass, RollInformation, SpecialLawyerTeam, SpecialTeamItem } from './types/home'
import type { PageParams, PageResult } from '@/http/types'
import { http } from '@/http/alova'

export function getLegalBanner() {
  return http.Post<HomeBannerItem[]>('/api/v1/home/legalBanner')
}
export function getHomeTitle() {
  return http.Post<SpecialLawyerTeam[]>('/api/v1/home/homeTitle')
}
export function getLegalClass() {
  return http.Post<LegalCategory[]>('/api/v1/home/legalClass')
}
export function getLegalSubClass() {
  return http.Post<CaseSubCategory[]>('/api/v1/home/legalSecondClass')
}
export function getLegalClassList() {
  return http.Post<LegalFirstClass[]>('/api/v1/home/legalClassList')
}
export function getLawyerList(pageParams: PageParams, lawyerTop?: number) {
  return http.Post<PageResult<LawyerDetail>>('/api/v1/home/lawyerList', {
    lawyerTop,
    page: pageParams.page,
    size: pageParams.pageSize,
  }, {
    transform(raw) {
      // raw 是 unknown，需要先断言
      const res = raw as {
        records?: LawyerDetail[]
        total?: number
        pages?: number
        size?: number
        [key: string]: any
      }

      return {
        list: res.records ?? [],
        total: res.total ?? 0,
        page: res.pages ?? pageParams.page,
        pageSize: res.size ?? pageParams.pageSize,
      } satisfies PageResult<LawyerDetail>
    },
  })
}
export function getLawyerSecondTabList(pageParams: PageParams, parentId: number, secondId?: number, sortBy?: number) {
  return http.Post<PageResult<LawyerDetailV2>>('/api/v1/home/lawyerSecondTabList', {
    parentId,
    secondId,
    sortBy,
    page: pageParams.page,
    size: pageParams.pageSize,
  }, {
    transform(raw) {
      // raw 是 unknown，需要先断言
      const res = raw as {
        records?: LawyerDetailV2[]
        total?: number
        pages?: number
        size?: number
        [key: string]: any
      }

      return {
        list: res.records ?? [],
        total: res.total ?? 0,
        page: res.pages ?? pageParams.page,
        pageSize: res.size ?? pageParams.pageSize,
      } satisfies PageResult<LawyerDetailV2>
    },
  })
}
export function getSpecialTeam() {
  return http.Post<SpecialTeamItem[]>('/api/v1/home/specialTeam')
}
export function getHotTags() {
  return http.Post<LegalSecondClass[]>('/api/v1/home/hotTags')
}
export function submitUserComment(comment?: LawyerComment) {
  return http.Post<boolean>('/api/v1/home/saveUserComment', { ...comment })
}
export function fetchRollInformation() {
  return http.Post<RollInformation[]>('/api/v1/home/roll')
}
export function searchLawyerInformList(pageParams: PageParams, esKey?: string) {
  return http.Post<PageResult<LawyerSearchHit>>('/api/v1/es/lawyer/infrom', {
    esKey,
    page: pageParams.page,
    size: pageParams.pageSize,
  }, {
    transform(raw, headers) {
      // raw 是 unknown，需要先断言
      const res = raw as {
        records?: LawyerSearchHit[]
        total?: number
        pages?: number
        size?: number
        [key: string]: any
      }

      return {
        list: res.records ?? [],
        total: res.total ?? 0,
        page: res.pages ?? pageParams.page,
        pageSize: res.size ?? pageParams.pageSize,
      } satisfies PageResult<LawyerSearchHit>
    },
  })
}

export function complaint(lawyerId?: number, content?: string) {
  return http.Post<boolean>('/api/v1/home/complaint', { lawyerId, content })
}
export function saveSearchLog(content?: string) {
  return http.Post<boolean>('/api/v1/mine/search_log', { content })
}
export function switchType(type?: number) {
  /**
   * 切换系统功能开关
   * @param {number} type - 开关类型，可选值：1(手机验证码开关)，2(OPPO审核开关)
   * @returns {Promise<boolean>} 返回操作是否成功
   */
  return http.Post<boolean>('/api/v1/home/switch', { type }) // 1:手机验证码开关，2：OPPO审核开关
}
export function fetchAreaList() {
  return http.Post<Area[]>('/api/v1/home/cities')
}
